package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.StExam;
import com.example.demo.entity.StExamQuestion;
import com.example.demo.entity.StQuestion;
import com.example.demo.entity.StQuestionOption;
import com.example.demo.entity.request.StQuestionGenerateRequest;
import com.example.demo.entity.response.StQuestionGenerateResponse;
import com.example.demo.entity.response.StQuestionResponse;
import com.example.demo.mapper.StExamQuestionMapper;
import com.example.demo.mapper.StQuestionMapper;
import com.example.demo.mapper.StQuestionOptionMapper;
import com.example.demo.service.StExamService;
import com.example.demo.mapper.StExamMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.*;

@Service
public class StExamServiceImpl extends ServiceImpl<StExamMapper, StExam>
    implements StExamService{
    @Autowired
    StQuestionMapper questionMapper;
    @Autowired
    StExamQuestionMapper examQuestionMapper;
    @Autowired
    StQuestionOptionMapper questionOptionMapper;

    @Override
    public StQuestionGenerateResponse generate(StQuestionGenerateRequest request) {
        List<StQuestion> result = new ArrayList<>();
        try{
            Field[] fields = request.getClass().getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                if (field.get(request) == null) continue;

                int a = Integer.parseInt(field.getName().substring(4));
                LambdaQueryWrapper<StQuestion> wrapper = Wrappers.lambdaQuery();
                wrapper.eq(StQuestion::getType, a);
                List<StQuestion> list = questionMapper.selectList(wrapper);
                if (a > list.size()) return null;
                Set<Integer> set = generateRandomNumbers(a, list.size());
                for (Integer i : set) {
                    result.add(list.get(i));
                }
            }
        }
        catch (Exception ex){
            return null;
        }
        StQuestionGenerateResponse response = new StQuestionGenerateResponse();
        response.setQuestions(result);
        return response;
    }

    @Override
    public List<StQuestionResponse> select(Integer id) {
        LambdaQueryWrapper<StExamQuestion> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StExamQuestion::getExamId, id);
        List<StExamQuestion> list = examQuestionMapper.selectList(wrapper);

        List<StQuestionResponse> result = new ArrayList<>();

        for (StExamQuestion item : list) {
            StQuestionResponse questionResponse = new StQuestionResponse();
            StQuestion question = questionMapper.selectById(item.getQuestionId());
            BeanUtils.copyProperties(question, questionResponse);

            List<StQuestionOption> options = questionOptionMapper.getByQuestionId(question.getId());
            questionResponse.setOptions(options);

            result.add(questionResponse);
        }

        return result;
    }

    public static Set<Integer> generateRandomNumbers(int n, int max) {
        if (n > max) {
            throw new IllegalArgumentException("n must be less than or equal to max");
        }

        Set<Integer> set = new HashSet<>();
        Random random = new Random();

        while (set.size() < n) {
            int num = random.nextInt(max);
            set.add(num);
        }

        return set;
    }
}




